#include "StdAfx.h"
#include "Skydome.h"

Skydome::Skydome(void)
{
	worldMatrix.SetIdentity();

}

Skydome::~Skydome(void)
{
}


int Skydome::Load( char* file_nfg, char* positive_x_tga, char* negative_x_tga, char* positive_y_tga,
									char* negative_y_tga, char* positive_z_tga, char* negative_z_tga)
{
	if (model.Load(file_nfg) == LOAD_FAILED)
		return LOAD_FAILED;

	glGenTextures(1, &texture_id);
	glBindTexture(GL_TEXTURE_CUBE_MAP, texture_id);
	int width, height, bpp;

	char* imgData1 = LoadTGA(positive_x_tga, &width, &height, &bpp);
	glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE,
			imgData1);
	char* imgData2 = LoadTGA(negative_x_tga, &width, &height, &bpp);
	glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE,
			imgData2);
	char* imgData3 = LoadTGA(positive_y_tga, &width, &height, &bpp);
	glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE,
			imgData3);
	char* imgData4 = LoadTGA(negative_y_tga, &width, &height, &bpp);
	glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE,
			imgData4);
	char* imgData5 = LoadTGA(positive_z_tga, &width, &height, &bpp);
	glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE,
			imgData5);
	char* imgData6 = LoadTGA(negative_z_tga, &width, &height, &bpp);
	glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z , 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE,
			imgData6);
	delete[] imgData1;
	delete[] imgData2;
	delete[] imgData3;
	delete[] imgData4;
	delete[] imgData5;
	delete[] imgData6;

	glGenBuffers(1, &vbo);
	glBindBuffer(GL_ARRAY_BUFFER, vbo);
	glBufferData(GL_ARRAY_BUFFER, model.num_vertices * sizeof(Vertex), model.vertices, GL_STATIC_DRAW);
	glBindBuffer(GL_ARRAY_BUFFER, 0);

	glGenBuffers(1, &ibo);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, model.num_indices * sizeof(GLushort), model.indices, GL_STATIC_DRAW);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
	
	myShaders.Init("../Resources/Shaders/SkydomeShaderVS.vs", "../Resources/Shaders/SkydomeShaderFS.fs");
	return LOAD_SUCCEEDED;
}

void Skydome::Draw( ESContext *esContext, Camera * camera)
{
	glUseProgram(myShaders.program);
	// Open buffer
	glBindBuffer(GL_ARRAY_BUFFER, vbo);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);

	// Pass Vertex to Shaders
	glEnableVertexAttribArray(myShaders.positionAttribute);
	glVertexAttribPointer(myShaders.positionAttribute, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);

	// Pass Texture
	glActiveTexture(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_CUBE_MAP, texture_id);
	glUniform1i(myShaders.textureUniform, 0);

	// Pass MVP Matrix
	Matrix mvp = getWorldMatrix() * camera->getViewMatrix() * camera->getProjectionMatrix();
	glUniformMatrix4fv(myShaders.matMVPUniform, 1, GL_FALSE, &(mvp.m[0][0]));

	// Pass Index
	int size;
	glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
	glBindBuffer(GL_ARRAY_BUFFER, 0);

	// Draw
	glDrawElements(GL_TRIANGLES, size / sizeof(GLushort), GL_UNSIGNED_SHORT, 0);

	// Release buffer
	glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
	glBindBuffer(GL_ARRAY_BUFFER, 0);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}

void Skydome::Update ( ESContext *esContext, float deltaTime)
{
}
